<html>
<head><meta charset="utf-8"><title>DST layout if member is a DST slice · t-lang/wg-unsafe-code-guidelines · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/index.html">t-lang/wg-unsafe-code-guidelines</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/DST.20layout.20if.20member.20is.20a.20DST.20slice.html">DST layout if member is a DST slice</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="199867955"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/DST%20layout%20if%20member%20is%20a%20DST%20slice/near/199867955" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Igor Matuszewski <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/DST.20layout.20if.20member.20is.20a.20DST.20slice.html#199867955">(Jun 05 2020 at 12:22)</a>:</h4>
<p>Hi everyone!</p>
<p>I know that DST layouts is a touchy/subtle topic, in particular if dyn traits are involved (there's a lot of RFCs involved IIRC).<br>
It seems that currently the DSTs which have a member which is a slice have the same "metadata" as its unsized member... Would it be feasible to also specify that in the UCG, in addition to the bare slice (ptr, elem_count) layout?</p>
<p>An example:</p>
<div class="codehilite"><pre><span></span><code><span class="cp">#[repr(transparent)]</span><span class="w"></span>
<span class="k">struct</span> <span class="nc">Newtype</span><span class="p">([</span><span class="kt">u8</span><span class="p">]);</span><span class="w"></span>

<span class="k">struct</span> <span class="nc">MyDst</span><span class="o">&lt;</span><span class="n">H</span><span class="o">&gt;</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">  </span><span class="n">header</span>: <span class="nc">H</span><span class="p">,</span><span class="w"></span>
<span class="w">  </span><span class="n">tail</span>: <span class="nc">Newtype</span><span class="p">,</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>



<a name="199868181"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/DST%20layout%20if%20member%20is%20a%20DST%20slice/near/199868181" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Igor Matuszewski <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/DST.20layout.20if.20member.20is.20a.20DST.20slice.html#199868181">(Jun 05 2020 at 12:24)</a>:</h4>
<p>... which would have the same metadata as <code>struct Another&lt;H&gt; { header: H, tail: [u8] }</code></p>



<a name="200515722"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/DST%20layout%20if%20member%20is%20a%20DST%20slice/near/200515722" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/DST.20layout.20if.20member.20is.20a.20DST.20slice.html#200515722">(Jun 11 2020 at 08:10)</a>:</h4>
<blockquote>
<p>Would it be feasible to also specify that in the UCG, in addition to the bare slice (ptr, elem_count) layout?</p>
</blockquote>
<p>note that even bare slice layout is not specified</p>



<a name="200515753"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/DST%20layout%20if%20member%20is%20a%20DST%20slice/near/200515753" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/DST.20layout.20if.20member.20is.20a.20DST.20slice.html#200515753">(Jun 11 2020 at 08:11)</a>:</h4>
<p><span class="user-mention" data-user-id="153740">@Igor Matuszewski</span>  use <code>from_raw_parts</code> and <code>.as_ptr()</code>/<code>.len()</code> do convert between a slice and its pieces</p>



<a name="200515791"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/DST%20layout%20if%20member%20is%20a%20DST%20slice/near/200515791" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/DST.20layout.20if.20member.20is.20a.20DST.20slice.html#200515791">(Jun 11 2020 at 08:11)</a>:</h4>
<p>as for guaranteeing layout when the unsized tail is a slice... it would take someone to push through an RFC. I don't know what the concern are with stabilizing that.</p>



<a name="200539248"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/DST%20layout%20if%20member%20is%20a%20DST%20slice/near/200539248" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Igor Matuszewski <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/DST.20layout.20if.20member.20is.20a.20DST.20slice.html#200539248">(Jun 11 2020 at 12:50)</a>:</h4>
<p><span class="user-mention" data-user-id="120791">@RalfJ</span> Yes, I've been using <code>from_raw_parts</code> but I've been missing the guarantee for slice &lt;-&gt; slice "object" conversion, that the slice ptr "metadata" is basically the same as the metadata for the slice object pointer</p>



<a name="200539341"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/DST%20layout%20if%20member%20is%20a%20DST%20slice/near/200539341" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Igor Matuszewski <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/DST.20layout.20if.20member.20is.20a.20DST.20slice.html#200539341">(Jun 11 2020 at 12:51)</a>:</h4>
<p>Do you think it'd even make sense to separately define something like a "slice object"? I think I've seen this term used once or twice in the codebase when I tried to see how we handle these types of objects, but the reference/docs don't specify them at all and just speaks about either just "DST" or "trait object"</p>



<a name="200539461"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/DST%20layout%20if%20member%20is%20a%20DST%20slice/near/200539461" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Igor Matuszewski <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/DST.20layout.20if.20member.20is.20a.20DST.20slice.html#200539461">(Jun 11 2020 at 12:52)</a>:</h4>
<p>in particular, I couldn't write generic conversions for <code>?Sized</code> which are "slice objects" due to compiler not knowing that <code>slice_ptr as *const Self</code> is actually valid</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>